/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.earthquake.bracing;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.common.ui.GenericBracingListBox;
import gov.fema.hazus.bdc.client.hazards.earthquake.EarthquakeConstants;
import gov.fema.hazus.bdc.shared.entity.Bracing;
import gov.fema.hazus.bdc.shared.entity.Building;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

public class BracingForm extends AbstractForm implements BuildingUpdate {

	private Label crippleWalls;
	private Label chimneys;
	private Label chimneyAnchored;
	private Label plumbingSystem;
	private Label mechanicalSystem;
	private Label electricalSystem;
	private Label suspendedCeiling;
	private Label mechanicalOnRoof;
	private Label rooftanks;
	
	// form fields
	private GenericBracingListBox formCrippleWalls = new GenericBracingListBox();
	private GenericBracingListBox formChimneys = new GenericBracingListBox();
	private GenericBracingListBox formChimneyAnchored = new GenericBracingListBox();
	private GenericAnswerListBox formPlumbingSystem = new GenericAnswerListBox();
	private GenericAnswerListBox formMechanicalSystem = new GenericAnswerListBox();
	private GenericAnswerListBox formElectricalSystem = new GenericAnswerListBox();
	private GenericAnswerListBox formSuspendedCeiling = new GenericAnswerListBox();
	private GenericBracingListBox formMechanicalOnRoof = new GenericBracingListBox();
	private GenericBracingListBox formRooftanks = new GenericBracingListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(9,2);
	
	// entities
	private Bracing bracing;
	
	// listeners
	private BracingListBoxChangeListener bracingListBoxChangeListener;
	private EarthquakeConstants constants;
	
	public BracingForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (EarthquakeConstants)GWT.create(EarthquakeConstants.class);
		tabTitle = constants.earthquakeBracingTabTitle();
		
		crippleWalls = new Label(constants.earthquakeBracingCrippleWalls());
		chimneys = new Label(constants.earthquakeBracingChimney());
		chimneyAnchored = new Label(constants.earthquakeBracingChimneyAnchored());
		plumbingSystem = new Label(constants.earthquakeBracingPlumbingSystem());
		mechanicalSystem = new Label(constants.earthquakeBracingMechanicalSystem());
		electricalSystem= new Label(constants.earthquakeBracingElectricalSystem());
		suspendedCeiling = new Label(constants.earthquakeBracingSuspendedCeiling());
		mechanicalOnRoof = new Label(constants.earthquakeBracingMechanicalOnRoof());
		rooftanks = new Label(constants.earthquakeBracingRooftanks());
		
		// add labels to form
		table.setWidget(0, 0, crippleWalls);
		table.setWidget(1, 0, chimneys);
		table.setWidget(2, 0, chimneyAnchored);
		table.setWidget(3, 0, plumbingSystem);
		table.setWidget(4, 0, mechanicalSystem);
		table.setWidget(5, 0, electricalSystem);
		table.setWidget(6, 0, suspendedCeiling);
		table.setWidget(7, 0, mechanicalOnRoof);
		table.setWidget(8, 0, rooftanks);
		
		// set up form fields
		bracing = building.getBracing();
		bracingListBoxChangeListener = new BracingListBoxChangeListener(bracing);
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formCrippleWalls);
		table.setWidget(1, 1, formChimneys);
		table.setWidget(2, 1, formChimneyAnchored);
		table.setWidget(3, 1, formPlumbingSystem);
		table.setWidget(4, 1, formMechanicalSystem);
		table.setWidget(5, 1, formElectricalSystem);
		table.setWidget(6, 1, formSuspendedCeiling);
		table.setWidget(7, 1, formMechanicalOnRoof);
		table.setWidget(8, 1, formRooftanks);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);		
		
			
	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.bracing = building.getBracing();

		// listeners
		bracingListBoxChangeListener.updateBracing(bracing);
		
		// reload forms
		loadForms();
		
	}
	
	private void loadForms()
	{
		
		if( bracing != null )
		{
			formCrippleWalls.setListItem( bracing.getCrippleWalls() );
			formCrippleWalls.setName(Bracing.CRIPPLEWALLS);
			
			formChimneys.setListItem( bracing.getChimneys() );
			formChimneys.setName(Bracing.CHIMNEYS);
			
			formChimneyAnchored.setListItem( bracing.getChimneyAnchored() );
			formChimneyAnchored.setName(Bracing.CHIMNEYANCHORED);
			
			formPlumbingSystem.setListItem( bracing.getPlumbingSystem() );
			formPlumbingSystem.setName(Bracing.PLUMBING);
			
			formMechanicalSystem.setListItem( bracing.getMechanicalSystem() );
			formMechanicalSystem.setName(Bracing.MECHANICAL);
			
			formElectricalSystem.setListItem( bracing.getElectricalSystem() );
			formElectricalSystem.setName(Bracing.ELECTRICAL);
			
			formSuspendedCeiling.setListItem( bracing.getSuspendedCeiling() );
			formSuspendedCeiling.setName(Bracing.SUSPENDEDCEILING);
			
			formMechanicalOnRoof.setListItem( bracing.getMechanicalOnRoof() );
			formMechanicalOnRoof.setName(Bracing.MECHANICALROOF);
			
			formRooftanks.setListItem( bracing.getRooftanks() );
			formRooftanks.setName(Bracing.ROOFTANKS);
		}
		
		if( building != null )
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formCrippleWalls.setEnabled(false);
	            formChimneys.setEnabled(false);
	            formChimneyAnchored.setEnabled(false);
	            formPlumbingSystem.setEnabled(false);
	            formMechanicalSystem.setEnabled(false);
	            formElectricalSystem.setEnabled(false);
	            formSuspendedCeiling.setEnabled(false);
	            formMechanicalOnRoof.setEnabled(false);
	            formRooftanks.setEnabled(false);
	        }
	        else
	        {
	            formCrippleWalls.setEnabled(true);
	            formChimneys.setEnabled(true);
	            formChimneyAnchored.setEnabled(true);
	            formPlumbingSystem.setEnabled(true);
	            formMechanicalSystem.setEnabled(true);
	            formElectricalSystem.setEnabled(true);
	            formSuspendedCeiling.setEnabled(true);
	            formMechanicalOnRoof.setEnabled(true);
	            formRooftanks.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formCrippleWalls.addChangeListener(bracingListBoxChangeListener);
		formChimneys.addChangeListener(bracingListBoxChangeListener);
		formChimneyAnchored.addChangeListener(bracingListBoxChangeListener);
		formPlumbingSystem.addChangeListener(bracingListBoxChangeListener);
		formMechanicalSystem.addChangeListener(bracingListBoxChangeListener);
		formElectricalSystem.addChangeListener(bracingListBoxChangeListener);
		formSuspendedCeiling.addChangeListener(bracingListBoxChangeListener);
		formMechanicalOnRoof.addChangeListener(bracingListBoxChangeListener);
		formRooftanks.addChangeListener(bracingListBoxChangeListener);
	}
}
